package main

import (
	"fmt"
	"time"
)

func main() {

	//500毫秒重复一次 它会每隔 500 毫秒向其 C 字段发送一个时间信号
	ticker := time.NewTicker(500 * time.Millisecond)
	done := make(chan bool)

	go func() {
		for {
			select {
			case <-done:
				return
			case t := <-ticker.C:
				fmt.Println("Tick at", t)
			case <-time.After(200 * time.Millisecond):
				fmt.Println("Timeout after 200 milliseconds")
			}
		}
	}()

	//主Goroutine休眠1600秒后把ticker0停止
	time.Sleep(1600 * time.Millisecond)
	ticker.Stop()

	done <- true //return
	fmt.Println("Ticker stopped")
}
